package com.huiquan.portal.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.huiquan.analysis.domain.User;
import com.huiquan.foundation.constant.RoleID;
import com.huiquan.onlineTime.service.OnlineTimeRecordService;

@Controller
public class LogOutController {

	@Autowired
	private OnlineTimeRecordService onlineTimeRecordService;

	private final Logger LOG = LoggerFactory.getLogger(LogOutController.class);

	@RequestMapping(value = "/logout.htm")
	public void doAction(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		if (user != null) {
			if (!user.getRole().equals(RoleID.ADMIN.getId())) {
				try {
					onlineTimeRecordService.logout(user, new Date());
				} catch (Exception e) {
					LOG.error("logout set online time error, uid=" + user.getUserId());
					e.printStackTrace();
				}
			}
			req.getSession().removeAttribute("user");
			LOG.info("logout success;userName=" + user.getUserName());
		}

		resp.sendRedirect("/login.htm");
	}

}
